/**
 * 
 */
package com.tvshows.front.controllers.dialog;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.swt.graphics.Drawable;

import com.tvshows.front.factory.TypeUI;
import com.tvshows.front.factory.ViewFactory;
import com.tvshows.front.ui.FrontUI;
import com.tvshows.front.ui.dialog.ActionsUI;
import com.tvshows.front.ui.recap.RecapUI;
import com.tvshows.front.ui.recap.UpsideCompositeRecapUI;
import com.tvshows.front.view.dialog.ActionsView;
import com.tvshows.interactions.ObserverBdd;
import com.tvshows.structure.bdd.BddQueries;
import com.tvshows.structure.beans.Favorite;
import com.tvshows.structure.log.LoggingTreatment;
import com.tvshows.structure.reader.FileOperations;
import com.tvshows.structure.xml.XmlExporter;

/**
 * @author tMidy
 * <p>
 * Executes actions from the popup widgets
 * </p>
 */
public class ActionsController
{

    /**
     * The {@link ActionsView}
     */
    private static ActionsUI actionsView;

    /**
     * Logger of the class
     */
    private static Logger logger = Logger.getLogger( ActionsController.class );

    /**
     * Adds a live link to the table
     * 
     * @param upsideView the {@link UpsideCompositeRecapUI}
     * @param recapView the {@link RecapUI}
     */
    public static void addLiveLink( UpsideCompositeRecapUI upsideView, Drawable recapView )
    {
        try
        {
            Runtime runtime = Runtime.getRuntime();
            String softwarePath = new FileOperations().readProperties().get( 0 );
            runtime.exec( softwarePath + " " + upsideView.getAddShowInLive().getText().replace( " ", "." ) );
            ( (RecapUI) recapView ).getTableComposite().addToTable( upsideView.getAddShowInLive().getText() );
            upsideView.getAddShowInLive().setText( "" );
        }
        catch ( Exception e )
        {
            LoggingTreatment.getInstance().logError( logger, e.getMessage() );
        }

    }

    /**
     * Opens log popup
     * 
     * @param view the frontUI
     */
    public static void openLog( FrontUI view )
    {
        actionsView = (ActionsUI) ViewFactory.getUI( TypeUI.ACTIONS_VIEW );
        actionsView.openMenuItem( view, TypeUI.LOG_VIEW );
    }

    /**
     * Opens options menu
     * 
     * @param view the frontUI
     */
    public static void openOptions( FrontUI view )
    {
        actionsView = (ActionsUI) ViewFactory.getUI( TypeUI.ACTIONS_VIEW );
        actionsView.openMenuItem( view, TypeUI.OPTION_VIEW );

    }

    /**
     * Exits the program
     */
    public static void exit()
    {
        System.exit( 0 );
    }

    /**
     * Export the favorite list to xml file
     * 
     * @param path the path to save
     */
    public static void exportXML( String path )
    {
        try
        {
            XmlExporter.exportToXml( BddQueries.getBddQueries().getFavoritesList(), path );
        }
        catch ( Exception e )
        {
            LoggingTreatment.getInstance().logError( logger, e.getMessage() );
        }

    }

    /**
     * Export the favorite list to xml file
     * 
     * @param path the path to save
     */
    public static boolean importXml( String path )
    {
        try
        {
            List<Favorite> favorites = XmlExporter.retreiveImportList( path );
            BddQueries.getBddQueries().getFavoritesList().clear();
            BddQueries.getBddQueries().getFavoritesList().addAll( favorites );

            ObserverBdd.getObserverBdd().reloadListFavorite();
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            LoggingTreatment.getInstance().logError( logger, "Erreur lors de l'import du fichier" );
            return false;
        }

        return true;

    }

    /**
     * Active le mode Sql
     */
    public static void activeSql()
    {
        BddQueries.getBddQueries().activeSql();

    }

    /**
     * Opens the job running popup
     * 
     * @param view the view to open
     */
    public static void popUpJobRunning( FrontUI view )
    {
        actionsView = (ActionsUI) ViewFactory.getUI( TypeUI.ACTIONS_VIEW );
        actionsView.openJobRunningPopUp( view );

    }

}
